﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowersourceUIToolkit;
using UCL.Powersource.Helpers;
using UCL.Powersource.TFS;

namespace UCL.Powersource.ViewModels
{
    [Export(typeof(SettingsViewModel))]
    public class SettingsViewModel : ViewModelBase
    {
        private PowersourceMainViewModel _PowersourceMainViewModel;

        private IPackageConfiguration CurrentPackageConfiguration
        {
            get
            {
                if (_PowersourceMainViewModel == null)
                    return null;
                else
                    return _PowersourceMainViewModel.PackageConfiguration;
            }
        }

        #region CurrentTfsTeamProjectCollectionConfiguration

        /// <summary>
        /// Property CurrentTfsTeamProjectCollectionConfiguration. TODODOC: Insert appropriate summary here
        /// </summary>
        public ITfsTeamProjectCollectionConfiguration CurrentTfsTeamProjectCollectionConfiguration
        {
            get
            {
                if (_PowersourceMainViewModel == null)
                    return null;
                else
                    return _PowersourceMainViewModel.CurrentTfsTeamProjectCollectionConfiguration;
            }
        }

        private void CurrentTfsTeamProjectCollectionConfiguration_RecalculateDependencies()
        {
            IldasmPath_Recalculate();
            LocalDropBasePath_Recalculate();
            CodeIndexDBServer_Recalculate();
            ExportPath_Recalculate();
            WebServiceUrl_Recalculate();
            DefaultProjectName_Recalculate();
            NotifyOfPropertyChange(() => CurrentTfsTeamProjectCollectionConfiguration);
        }
        #endregion

        #region IldasmPath

        /// <summary>
        /// Field storage for property IldasmPath
        /// </summary>
        private string _IldasmPath = default(string);

        /// <summary>
        /// Property IldasmPath. TODODOC: Insert appropriate summary here
        /// </summary>
        public string IldasmPath
        {
            get { return _IldasmPath; }
            set { if (_IldasmPath == value) return; _IldasmPath = value; IldasmPath_RecalculateDependencies(); }
        }

        private void IldasmPath_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => IldasmPath);
        }

        private void IldasmPath_Recalculate()
        {
            if (CurrentTfsTeamProjectCollectionConfiguration != null)
                _IldasmPath = CurrentTfsTeamProjectCollectionConfiguration.IldasmPath;
            //Insert your code here

            IldasmPath_RecalculateDependencies();
        }

        #endregion


        #region LocalDropBasePath

        /// <summary>
        /// Field storage for property LocalDropBasePath
        /// </summary>
        private string _LocalDropBasePath = default(string);

        /// <summary>
        /// Property LocalDropBasePath. TODODOC: Insert appropriate summary here
        /// </summary>
        public string LocalDropBasePath
        {
            get { return _LocalDropBasePath; }
            set { if (_LocalDropBasePath == value) return; _LocalDropBasePath = value; LocalDropBasePath_RecalculateDependencies(); }
        }

        private void LocalDropBasePath_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => LocalDropBasePath);
        }

        private void LocalDropBasePath_Recalculate()
        {
            if (CurrentTfsTeamProjectCollectionConfiguration != null)
                _LocalDropBasePath = CurrentTfsTeamProjectCollectionConfiguration.LocalDropBasePath;
            //Insert your code here

            LocalDropBasePath_RecalculateDependencies();
        }

        #endregion


        #region CodeIndexDBServer

        /// <summary>
        /// Field storage for property CodeIndexDBServer
        /// </summary>
        private string _CodeIndexDBServer = default(string);

        /// <summary>
        /// Property CodeIndexDBServer. TODODOC: Insert appropriate summary here
        /// </summary>
        public string CodeIndexDBServer
        {
            get { return _CodeIndexDBServer; }
            set { if (_CodeIndexDBServer == value) return; _CodeIndexDBServer = value; CodeIndexDBServer_RecalculateDependencies(); }
        }

        private void CodeIndexDBServer_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => CodeIndexDBServer);
        }

        private void CodeIndexDBServer_Recalculate()
        {
            if (CurrentTfsTeamProjectCollectionConfiguration != null)
                _CodeIndexDBServer = CurrentTfsTeamProjectCollectionConfiguration.CodeIndexDBServer;
            //Insert your code here

            CodeIndexDBServer_RecalculateDependencies();
        }

        #endregion


        #region ExportPath

        /// <summary>
        /// Field storage for property ExportPath
        /// </summary>
        private string _ExportPath = default(string);

        /// <summary>
        /// Property ExportPath. TODODOC: Insert appropriate summary here
        /// </summary>
        public string ExportPath
        {
            get { return _ExportPath; }
            set { if (_ExportPath == value) return; _ExportPath = value; ExportPath_RecalculateDependencies(); }
        }

        private void ExportPath_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => ExportPath);
        }

        private void ExportPath_Recalculate()
        {
            if (CurrentTfsTeamProjectCollectionConfiguration != null)
                _ExportPath = CurrentTfsTeamProjectCollectionConfiguration.ExportPath;
            //Insert your code here

            ExportPath_RecalculateDependencies();
        }

        #endregion


        #region WebServiceUrl

        /// <summary>
        /// Field storage for property WebServiceUrl
        /// </summary>
        private string _WebServiceUrl = default(string);

        /// <summary>
        /// Property WebServiceUrl. TODODOC: Insert appropriate summary here
        /// </summary>
        public string WebServiceUrl
        {
            get { return _WebServiceUrl; }
            set { if (_WebServiceUrl == value) return; _WebServiceUrl = value; WebServiceUrl_RecalculateDependencies(); }
        }

        private void WebServiceUrl_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => WebServiceUrl);
        }

        private void WebServiceUrl_Recalculate()
        {
            if (CurrentTfsTeamProjectCollectionConfiguration != null)
                _WebServiceUrl = CurrentTfsTeamProjectCollectionConfiguration.WebServiceUrl;
            //Insert your code here

            WebServiceUrl_RecalculateDependencies();
        }

        #endregion


        #region DefaultProjectName

        /// <summary>
        /// Field storage for property DefaultProjectName
        /// </summary>
        private string _DefaultProjectName = default(string);

        /// <summary>
        /// Property DefaultProjectName. TODODOC: Insert appropriate summary here
        /// </summary>
        public string DefaultProjectName
        {
            get { return _DefaultProjectName; }
            set { if (_DefaultProjectName == value) return; _DefaultProjectName = value; DefaultProjectName_RecalculateDependencies(); }
        }

        private void DefaultProjectName_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => DefaultProjectName);
        }

        private void DefaultProjectName_Recalculate()
        {
            if (CurrentTfsTeamProjectCollectionConfiguration != null)
                _DefaultProjectName = CurrentTfsTeamProjectCollectionConfiguration.DefaultProjectName;
            //Insert your code here

            DefaultProjectName_RecalculateDependencies();
        }

        #endregion



        protected override bool OnInitializing()
        {
            _PowersourceMainViewModel = ViewModelLocator.GetViewModel<PowersourceMainViewModel>();

            _PowersourceMainViewModel.PropertyChanged -= _PowersourceMainViewModel_PropertyChanged;
            _PowersourceMainViewModel.PropertyChanged += _PowersourceMainViewModel_PropertyChanged;

            return true;
        }

        private void _PowersourceMainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == PropertyExtensions.GetPropertyName(() => _PowersourceMainViewModel.CurrentTfsTeamProjectCollectionConfiguration))
            {
                CurrentTfsTeamProjectCollectionConfiguration_RecalculateDependencies();
            }
        }

        protected override bool OnActivating()
        {
            CurrentTfsTeamProjectCollectionConfiguration_RecalculateDependencies();
            return true;
        }

        protected override bool OnDeactivating()
        {
            return true;
        }

        protected override void OnDisposing()
        {
            if (_PowersourceMainViewModel != null)
            {
                _PowersourceMainViewModel.PropertyChanged -= _PowersourceMainViewModel_PropertyChanged;

                _PowersourceMainViewModel = null;
            }
        }

        public void SaveChanges()
        {
            if (CurrentTfsTeamProjectCollectionConfiguration != null)
            {
                CurrentTfsTeamProjectCollectionConfiguration.IldasmPath = IldasmPath;
                CurrentTfsTeamProjectCollectionConfiguration.LocalDropBasePath = LocalDropBasePath;
                CurrentTfsTeamProjectCollectionConfiguration.CodeIndexDBServer = CodeIndexDBServer;
                CurrentTfsTeamProjectCollectionConfiguration.ExportPath = ExportPath;
                CurrentTfsTeamProjectCollectionConfiguration.WebServiceUrl = WebServiceUrl;
                CurrentTfsTeamProjectCollectionConfiguration.DefaultProjectName = DefaultProjectName;
            }

            Exit();
        }

        public event EventHandler FireDefaultDialog;

        public readonly string DefaultDialogMsg = "Are you sure you want to reset the settings to their default values?";

        public readonly string DefaultDialogCaption = "Reset to Default Settings";

        public void RequestResetToDefaults()
        {
            if (CurrentPackageConfiguration != null && CurrentTfsTeamProjectCollectionConfiguration != null &&
                (IldasmPath != CurrentPackageConfiguration.DefaultIldasmPath ||
                LocalDropBasePath != CurrentPackageConfiguration.DefaultLocalDropBasePath ||
                CodeIndexDBServer != CurrentPackageConfiguration.DefaultCodeIndexDBServer ||
                ExportPath != CurrentPackageConfiguration.DefaultExportPath ||
                WebServiceUrl != CurrentPackageConfiguration.CreateDefaultWebServiceUrl(CurrentTfsTeamProjectCollectionConfiguration.Uri) ||
                DefaultProjectName != CurrentTfsTeamProjectCollectionConfiguration.CalculateDefaultProjectName()))
            {
                if (FireDefaultDialog != null) FireDefaultDialog(this, EventArgs.Empty);
            }
        }

        public void ResetToDefaults()
        {
            if (CurrentPackageConfiguration != null && CurrentTfsTeamProjectCollectionConfiguration != null)
            {
                IldasmPath = CurrentPackageConfiguration.DefaultIldasmPath;
                LocalDropBasePath = CurrentPackageConfiguration.DefaultLocalDropBasePath;
                CodeIndexDBServer = CurrentPackageConfiguration.DefaultCodeIndexDBServer;
                ExportPath = CurrentPackageConfiguration.DefaultExportPath;
                WebServiceUrl = CurrentPackageConfiguration.CreateDefaultWebServiceUrl(CurrentTfsTeamProjectCollectionConfiguration.Uri);
                DefaultProjectName = CurrentTfsTeamProjectCollectionConfiguration.CalculateDefaultProjectName();
            }
        }

        public event EventHandler FireSaveDialog;

        public readonly string SaveDialogMsg = "Do you want to save the changes to the settings before closing this window?";

        public readonly string SaveDialogCaption = "Settings";

        public void RequestExit()
        {
            if (CurrentTfsTeamProjectCollectionConfiguration != null &&
                (IldasmPath != CurrentTfsTeamProjectCollectionConfiguration.IldasmPath ||
                 LocalDropBasePath != CurrentTfsTeamProjectCollectionConfiguration.LocalDropBasePath ||
                 CodeIndexDBServer != CurrentTfsTeamProjectCollectionConfiguration.CodeIndexDBServer ||
                 ExportPath != CurrentTfsTeamProjectCollectionConfiguration.ExportPath ||
                 WebServiceUrl != CurrentTfsTeamProjectCollectionConfiguration.WebServiceUrl ||
                 DefaultProjectName != CurrentTfsTeamProjectCollectionConfiguration.DefaultProjectName))
            {
                if (FireSaveDialog != null) FireSaveDialog(this, EventArgs.Empty);
                return;
            }

            Exit();

        }

        public void Exit()
        {
            IViewModel main = ViewModelLocator.GetViewModel<PowersourceMainViewModel>();
            if (!main.IsInitialized) main.Initialize();
            IUIManager ui = IoC.Get<IUIManager>();
            ui.ActivateViewModelInConductor(main, ui.GetConductorForViewModel(this));
        }
    }
}
